package com.sls.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.sls.mall.common.exception.SLSCodeEnume;
import com.sls.mall.member.exception.PhoneExsitException;
import com.sls.mall.member.exception.UsernameExistException;
import com.sls.mall.member.feign.CouponFeignService;
import com.sls.mall.member.vo.MemberLoginVO;
import com.sls.mall.member.vo.MemberRegistVO;
import com.sls.mall.member.vo.MemberRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sls.mall.member.entity.MemberEntity;
import com.sls.mall.member.service.MemberService;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.R;



/**
 * 
 *
 * @author tg
 * @email tg@gmail.com
 * @date 2021-04-24 16:44:04
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;


    /**
     *
     * @param memberLoginVO
     * @return
     *
     *  如果传入的参数结果是 json 才需要用 @RequestBody 转换为实体类
     */
    @RequestMapping("/login")
    public R login(@RequestBody MemberLoginVO memberLoginVO)
    {
        MemberEntity entity = memberService.login(memberLoginVO);

        MemberRespVO respVO = new MemberRespVO();

        BeanUtils.copyProperties(entity,respVO);

        if (entity != null)
        {
            return R.ok().put("data",respVO);
        }else {
            return R.error(SLSCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),SLSCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
        }

    }


    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVO memberRegistVo)
    {
        try{

            memberService.regist(memberRegistVo);

            return R.ok();
        }catch (PhoneExsitException e)
        {
            return R.error(SLSCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),SLSCodeEnume.PHONE_EXIST_EXCEPTION.getMessage());

        }catch (UsernameExistException e)
        {

            return R.error(SLSCodeEnume.USER_EXIST_EXCEPTION.getCode(),SLSCodeEnume.USER_EXIST_EXCEPTION.getMessage());
        }

    }

    @RequestMapping("/testMember")
    public R test(){

        MemberEntity entity = new MemberEntity();
        entity.setNickname("莉莉");

        R r = couponFeignService.testList();

        return R.ok().put("member",entity).put("coupons",r);

    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
